import Vue from 'vue'
import Vuex from 'vuex'
import shouye from './shouye'
import topic from './topic'
import axios from 'axios'
Vue.use(Vuex)
import brochure from '../data/brochure.json'
export default new Vuex.Store({
  // 初始化数据
  state: {
    isShow:false,
    shangeTop:false,
    loginState:false,
    topicData:[],
    attention:[],
    myComisShow:false,
    labelData:[],
    userLabellData:[],
    showLabelData:[],
    brochure:brochure
  },
  // computed 计算属性，操作state
  getters:{ 
    
  },
  // 改变state，处理同步请求
  mutations: {
    //点击登录 登录弹框组件出现
    IsShowTrue(state){
      state.isShow=true
    },
    //点击登录组件右上角的x组件隐藏
    IsShowFalse(state){
      state.isShow=false
    },
    //右边的组件定位距离
    shangeTopTrue(state){
      state.shangeTop=true
    },
    //右边的组件定位距离
    shangeTopFalse(state){
      state.shangeTop=false
    },
    //改变登录的状态 成功后为true
    changeLoginState(state){
      state.loginState=true
    },
    //改变登录的状态 退出后为false
    changeLoginFalse(state){
      state.loginState=false
    },
    changgeTopicData(state,data){
      state.topicData =data
    },
    addAttention(state,data){
      state.attention=data
    },
    //点击头像
    myComisTrue(state){
      state.myComisShow=!state.myComisShow
    },
    //关闭myCom组件
    myComisFalse(state){
      state.myComisShow=false
    },
    //获取标签数据
    getLabel(state,data){
      state.labelData=data
      state.showLabelData=JSON.parse(JSON.stringify(state.labelData))
    },
    getUserLabel(state,data){
      state.userLabellData=data
    },
    filData(state,user){
      if(user==''){
        state.labelData=state.showLabelData
      }
      state.labelData=state.showLabelData
      state.labelData=state.showLabelData.filter(item=>item.tag.tag_name.includes(user) )
      
    }
  },
  // 改变mutations，处理异步请求
  actions: {
    register({commit},payload){
      console.log(commit,payload)
      axios.post("/register", payload)
      .then((res) => {
        console.log(res)
          if (res.data.code === 1) {
            alert("注册成功");
          } else {
            alert("注册失败");
          }
        });
    },
    login({commit},payload){
      axios.post("/login", payload)
      .then((res) => {
          if (res.data.code === 1) {
            localStorage.setItem("isLogin", payload.phone);
            alert("登陆成功");
            commit("IsShowFalse");
            commit("changeLoginState");
          } else {
            localStorage.removeItem("isLogin");
            alert("登录失败");
          }
        });
    },
    
    //话题组件加载数据
    getTopic({commit},payload){
        console.log(commit,payload)
        axios.post("/TopicData",payload).then(res=>{
          if(res.data.code===1){
            commit('changgeTopicData',res.data.msg)
          }
          
        })
    },
    //点击关注把数据通过axios写入一个json文件
    addAttention({dispatch},payload){
      axios.post("/addAttention",payload).then(res=>{
        console.log(res.data.msg)
        dispatch('getAttention')
      })
    },
    //获取我关注的话题
    getAttention({commit}){
      axios.post("/getAttention").then(res=>{
        if(res.data.code===0){
          commit('addAttention',res.data.msg)
        }
        
      })
    },
    //获取标签数据
    getLabel({commit},payload){
      axios.post("/getLabel",payload).then(res=>{
        if(res.data.code===0){
          commit('getLabel',res.data.msg.data)
        }
        
      })
    },
    addLabel({dispatch},payload){
      axios.post("/addLabel",payload).then(res=>{
        if(res.data.code===1){
          dispatch('getUserData')
        }
        
      })
    },
    //获取已关注标签
    getUserData({commit}){
      axios.post("/getUserLabel").then(res=>{
        if(res.data.code===0){
          commit('getUserLabel',res.data.data)
        }
      })
    }
  },
  // 划分模块
  modules: {
    shouye,
    topic,
  }
})
